package com.meko.tms.core.log.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.meko.tms.core.log.annotation.Log;
import com.meko.tms.core.mapper.LogMapper;
import com.meko.tms.core.log.service.LogService;
import com.meko.tms.utils.StringUtils;
import java.lang.reflect.Method;
import java.util.Date;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;

/**
 * @author meko
 */
@Slf4j
@Service
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    public void save(String username, String ip, ProceedingJoinPoint joinPoint, com.meko.tms.core.log.entity.Log logg) {

        if (logg == null) {
            return;
        }
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Log aopLog = method.getAnnotation(Log.class);
            logg.setDescription(aopLog.value());

            // 方法路径
            String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";

            String empName = "";
            StringBuilder params = new StringBuilder("{");
            //参数值
            Object[] argValues = joinPoint.getArgs();

            if (aopLog.flag()) {
                //参数名称
                String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
                if (argValues != null) {
                    for (int i = 0; i < argValues.length; i++) {
                        params.append(" ").append(argNames[i]).append(": ").append(argValues[i]);
                    }
                }
            }
            // 获取IP地址
            logg.setRequestIp(ip);

            String LOGIN_PATH = "login";
            if (LOGIN_PATH.equals(signature.getName()) && argValues != null) {
                try {
                    username = new JSONObject(argValues[0]).get("username").toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                username = "unknown";
            }
            logg.setAddress(StringUtils.getCityInfo(logg.getRequestIp()));
            logg.setMethod(methodName);
            logg.setUsername(username);
            logg.setParams(params + " }");
            logg.setCreateTime(new DateTime());
            logg.setEmpName(empName);
            logMapper.insert(logg);
        } catch (Exception e) {
            logg.setCreateTime(new Date());
            logg.setDescription("日志记录异常");
            logg.setLogType("ERROR");
            try {
                logMapper.insert(logg);
            } catch (Exception er) {
                log.error("日志记录失败:", JSON.toJSONString(er));
            }
        }
    }
}
